/**
 * creation: 21-jan-2008
 */
package demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.jfonia.harmony.ChordParser;
import org.jfonia.musicxml.CodedMusicXMLImportException;
import org.jfonia.musicxml.HarmonyParser;
import org.jfonia.musicxml.MusicXMLImportException;
import org.jfonia.musicxml.MusicXMLImporter;
import org.jfonia.musicxml.Score;
import org.jfonia.musicxml.ScoreReporter;
import org.jfonia.musicxml.SimpleDOMParser;
import org.jfonia.musicxml.SimpleEntityResolver;
import org.jfonia.musicxml.model.Harmony;
import org.jfonia.musicxml.model.ScorePartwise;
import org.jfonia.util.CommandParser;
import org.jfonia.util.Logger;

/**
 * @author Wijnand
 *
 */
public class ConsoleTest
{
	public static void main(String[] args)
	{
		if (args.length < 1)
			usage();
		else
		{
			try {
				Map<String,String> params = CommandParser.parseCmdParams(args, 1);
					// key-value pairs
				
				String url = args[0];	// the musicxml file
				
				PrintWriter outWriter;
				if (params.containsKey("lilyPath"))
					outWriter = new PrintWriter(new File(params.get("lilyPath")));
				else
					outWriter = new PrintWriter(System.out, true); // autoFlush
				
				PrintWriter logWriter;
				if (params.containsKey("logPath"))
					logWriter = new PrintWriter(new File(params.get("logPath")));
				else
					logWriter = null; // new PrintWriter(System.err, true); // autoFlush

				PrintWriter internalLogWriter;
				if (params.containsKey("internalLogPath"))
					internalLogWriter = new PrintWriter(new File(params.get("internalLogPath")));
				else
					internalLogWriter = null; // new PrintWriter(System.err, true); // autoFlush
				
				String voicePartId = params.get("voicePartId");
					// not present => null => causes auto-scan
				String harmonyPartId = params.get("harmonyPartId");
				 	// not present => null => causes auto-scan
		
				String localDTDPath	= params.get("localDTDPath");
					// not present => null => url specified in xml-header (http); 
				
				
				MusicXMLImporter importer = new MusicXMLImporter();
				Logger<Throwable> logger = importer.getLogger();
				Logger<Throwable> internalLogger = importer.getInternalLogger();
				CodedMusicXMLImportException.setBundle(ResourceBundle.getBundle("org.jfonia.musicxml.errormessages"));
				try {
					InputStream in = new FileInputStream(new File(url));
	
					// normal:
					// DocumentBuilderFactory fac =
					// DocumentBuilderFactory.newInstance();
					// DocumentBuilder builder = fac.newDocumentBuilder();
	
					// with xerces:
					SimpleDOMParser builder = new SimpleDOMParser();
	
					// if you want to use local dtd's:
					if (localDTDPath != null)
						builder.setEntityResolver(new SimpleEntityResolver(localDTDPath)); 
	
					ChordParser cp = new ChordParser("org.jfonia.musicxml.musicxmlchordtypes");
					HarmonyParser hp = new HarmonyParser(cp, logger);
					Harmony.setHarmonyParser(hp);
					importer.setHarmonyParser(hp);
					
					ScorePartwise xmlScore = importer.readScore(builder, in, voicePartId, harmonyPartId);
	
					Score score = new Score(xmlScore);
					
					System.out.println("----- report 1 -----");
					//ScoreReporter.report1(score);
					System.out.println("----- report 2 -----");
					ScoreReporter.report2(score);
					
					//new LilypondExport(new PrintWriter(System.out), true).writeScore(score, false, false, false);
					
					//Export.writeScorePartwise(score, outWriter, true, chords, lyrics, header);
				}
				catch (MusicXMLImportException e)
				{
					System.err.println("fatal error");
					e.printStackTrace();
					//logger.log(e);
				}
				catch (Exception e)
				{
					System.err.println("fatal error");
					e.printStackTrace();
					internalLogger.log(e);
				}
	
				try {
					logger.report(new PrintWriter(System.err));
					if (logWriter != null)
						logger.report(logWriter);
					
					internalLogger.report(new PrintWriter(System.err));
					if (internalLogWriter != null)
						internalLogger.report(internalLogWriter);
				}
				catch (MissingResourceException e)
				{
					logWriter.println("Problemen met coded exceptions: " + e.getMessage());
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	
	public static void usage()
	{
		System.err.println("usage: blabla...");
	}

}
